import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../component/swiper-component.dart';
import '../service/method.dart';
import 'dart:convert';
import 'package:url_launcher/url_launcher.dart';

class Index extends StatefulWidget {
  Index({Key key}) : super(key: key);

  _IndexState createState() => _IndexState();
}

class _IndexState extends State<Index> {
  String pageContent = '正在获取数据';

  // @override
  // void initState() {
  //   homePageContent().then((val) {
  //     print(val);
  //     setState(() {
  //      pageContent = val.toString(); 
  //     });
  //   });
  //   super.initState();
  // }

  @override
  Widget build(BuildContext context) {
     // 像素密度
    // ScreenUtil.pixeRatio
    // 设备的宽度
    // ScreenUtil.screenHeight
    // 设备的高度
    // ScreenUtil.screenWidth
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Scaffold(
      body: FutureBuilder(
        future: bannerContent(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var data = json.decode(snapshot.data.toString());
            print('--------------------------------------------');
            List<Map> swiper = (data['data']['list'] as List).cast();
            String imgUrl = 'https://png.pngtree.com/thumb_back/fh260/back_pic/02/65/14/5957873074946eb.jpg';
            print(swiper);
            return Column(
              children: <Widget>[
                SwiperComponent(swiperList: swiper),
                AdImage(ImageUrl: imgUrl,)
              ],
            );
          } else {
            return Center(child: Text('Loadding'),);
          }
        },
      ),
    );
  }
}

// 运营位导航
class TopNavigator extends StatelessWidget {
  final List navigatorList;
  TopNavigator(Key key, this.navigatorList):super(key: key);
  @override
  Widget _gridViewItemUI(BuildContext context, item) {
    return InkWell(
      onTap: () {print('hello inkwell');},
      child: Column(
        children: <Widget>[
          Image.network('src', width: ScreenUtil().setWidth(90),),
          Text(item['title'])
        ],
      ),
    );
  }
  // 内部的方法，会返回一个widget 内部方法，都是以下划线开始的_,

  @override
  Widget build(BuildContext context) {
    if (this.navigatorList.length > 10) {
      this.navigatorList.removeRange(10, this.navigatorList.length);
    }
    return Container(
      height: ScreenUtil().setHeight(300),
      padding:EdgeInsets.all(10),
      child: GridView.count(
        crossAxisCount: 5,
        padding: EdgeInsets.all(5),
        children: navigatorList.map((item) {
          return _gridViewItemUI(context, item);
        }).toList(),
      ),
    );
  }
}

// 广告图片

class AdImage extends StatelessWidget {
  final ImageUrl;
  const AdImage({Key key, this.ImageUrl}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 5, bottom: 5),
      child: Image.network(this.ImageUrl),
    );
  }
}

class CallPhone extends StatelessWidget {
  final imgSrc;
  final phoneNumber;
  const CallPhone({Key key, this.imgSrc, this.phoneNumber}) : super(key: key);

  void _lauchURL() async{
    String url = 'tel:'+ phoneNumber;

    if(await canLaunch(url)) {
      await launch(url);
    } else {
      throw '';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: InkWell(
        onTap:_lauchURL,
        child: Image.network(imgSrc),
      ),
    );
  }
}

// SingleChildScrollView 可以避免超出限制

class Recommend extends StatelessWidget {
  final List recommendList;
  const Recommend({Key key, this.recommendList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(),
    );
  }
}